package org.caro.actions.listener;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Random;

import javax.swing.ImageIcon;
import javax.swing.JOptionPane;

import org.caro.gui.ActionZonePanel;
import org.caro.gui.UtilityZonePanel;
import org.caro.utils.CheckWinner;
import org.caro.utils.Constants;
import org.caro.utils.Position;

public class AiModeExecute implements ActionListener {
	private ActionZonePanel actionZone;
	private UtilityZonePanel utilityZone;
	private boolean isLocked = true;
	private int move = 0;
	private int minColumn = 0;
	private int maxColumn = 0;
	private int minRow = 0;
	private int maxRow = 0;
	private int finalX;
	private int finalY;
	private ImageIcon icon1 = new ImageIcon(Constants.IMAGE_PLAYER_1);
	private ImageIcon icon2 = new ImageIcon(Constants.IMAGE_PLAYER_2);
	public AiModeExecute(ActionZonePanel actionZone,UtilityZonePanel utilityZone) {
		this.actionZone = actionZone;
		this.utilityZone = utilityZone;
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		for (int i = 0; i < Constants.MAX_COLUMNS; i++) {
            for (int j = 0; j < Constants.MAX_ROWS; j++) {
                if (e.getSource() == actionZone.getButton()[i][j] && actionZone.getMapButton()[i][j] == Constants.NULL) {
                	isLocked = false;
                	utilityZone.setTurnIconForPlayer2();
                	makeMovement(i, j, Constants.PLAYER1);
                    expandArea(i, j);
                    calculateForAI(i, j);
                    makeMovement(finalX, finalY, Constants.PLAYER2);
                    utilityZone.setTurnIconForPlayer1();
                }
            }
        }
	}

	public void rateData(int x, int y) {

        if (x - 1 < minColumn && x - 1 > 0) {
            minColumn = x - 1;
        }
        if (x + 1 > maxColumn && x + 1 <= Constants.MAX_COLUMNS) {
            maxColumn = x + 1;
        }
        if (y - 1 < minRow && y - 1 > 0) {
            minRow = y - 1;
        }
        if (y + 1 > maxRow && y - 1 <= Constants.MAX_ROWS) {
            maxRow = y + 1;
        }

    }

    public void checkCombo(int comboValue, int object) {
        for (int i = minColumn; i <= maxColumn; i++) {
            for (int j = minRow; j <= maxRow; j++) {
                if (actionZone.getMapButton()[i][j] == Constants.NULL) {
                	actionZone.getMapButton()[i][j]=object;
                    if (CheckWinner.winner(actionZone.getMapButton(),i,j,object) == object) {
                        finalX = i;
                        finalY = j;
                        isLocked = true;

                    }
                    actionZone.getMapButton()[i][j]=Constants.NULL;
                }
            }
        }
    }

    public void calculateForAI(int x, int y) {
        if (isLocked == false) {
            checkCombo(5, Constants.PLAYER1);
        }
        if (isLocked == false) {
            checkCombo(5, Constants.PLAYER2);
        }
        if (isLocked == false) {
            checkCombo(4, Constants.PLAYER1);
        }
        if (isLocked == false) {
            checkCombo(4, Constants.PLAYER2);
        }
        if (isLocked == false) {
            checkCombo(3, Constants.PLAYER2);
        }
        if (isLocked == false) {

            ArrayList<Position> randomPieceArray = new ArrayList<Position>();

            for (int i = x - 1; i <= x + 1; i++) {
                for (int j = y - 1; j <= y + 1; j++) {

                    if (i >= 0 && i < 20 && j >= 0 && j < 20
                            && actionZone.getMapButton()[i][j] == 0) {

                        Position p = new Position(i, j);
                        randomPieceArray.add(p);
                    }
                }
            }
            Random rd = new Random();
            Position tmp = randomPieceArray.get(rd.nextInt(randomPieceArray
                    .size()));
            finalX = tmp.getX();
            finalY = tmp.getY();
            isLocked = true;
        }
    }

    public void expandArea(int x, int y) {
        if (move == 1) {
            if (x != 0) {
                minColumn = x - 1;
            } else {
                minColumn = x;
            }
            if (x != 19) {
                maxColumn = x + 1;
            } else {
                maxColumn = x;
            }

            if (y != 0) {
                minRow = y - 1;
            } else {
                minRow = y;
            }

            if (y != 19) {
                maxRow = y + 1;
            } else {
                maxRow = y;
            }
        } else {
            rateData(x, y);
        }
    }

    public void makeMovement(int x, int y, int object) {
        ImageIcon defaultIcon;
        int defaultObject;
        // //////////
        if (object == Constants.PLAYER1) {
            defaultIcon = icon1;
            defaultObject = Constants.PLAYER1;

        } else {
            defaultIcon = icon2;
            defaultObject = Constants.PLAYER2;

        }
        actionZone.getMapButton()[x][y] = object;
        actionZone.getButton()[x][y].setIcon(defaultIcon);
        move++;
        /// SET  LABEL HERE ...
      //  boardInterface.setLuotDi("Lượt đi:  " + move);
        
        if (CheckWinner.winner(actionZone.getMapButton(), x, y, 5) == defaultObject) {
            if (defaultObject == Constants.PLAYER1) {
                JOptionPane.showMessageDialog(null,
                        "Xin chúc mừng , bạn đã chiến thắng ! ");
               utilityZone.addPointForPLayer1();
                /// SET  LABEL HERE ...
               // boardInterface.setLb_Diem_Player1(score1);
            } else {
                JOptionPane.showMessageDialog(null, "Bạn đã thua ! ");
                utilityZone.addPointForPlayer2();
                /// SET  LABEL HERE ...
                //boardInterface.setLb_Diem_Player2(score2);
            }
            actionZone.setDefaultBoard();
            /// SET  LABEL HERE ...
            //boardInterface.setDefaultBoard();

            move = 0;
            /// SET  LABEL HERE ...
           // boardInterface.setLuotDi("Lượt đi:  " + String.valueOf(move));

        }
    }
}
